/*
 * FileName: AdminController.java
 * Author:   庾宗耀
 * Date:     2015年1月29日 上午9:50:22
 * Description: 管理员添加、修改用户等的实现
 * History:
 * <author>   <time>    <version>    <desc>
 * 修改人姓名                 修改时间                 版本号                               描述
 */
package com.linkage.admin.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.linkage.admin.service.AdminService;
import com.linkage.book.pojo.Book;
import com.linkage.user.interceptor.NeedLogin;
import com.linkage.user.pojo.User;
import com.linkage.user.service.UserService;

/**
 * @author 庾宗耀
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    /**
     * 
     * @param model
     * @return 查询所有的用户信息(List<User>)
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    //@NeedLogin
    public String list(Model model) {
        List<User> users = this.adminService.queryAll();
        model.addAttribute("users", users);

        return "user/list";
    }

    /**
     * 
     * @param model
     * @param userId
     * @return 跳转到更新后的页面
     */
    @RequestMapping(value = "/updateView/{userId}", method = RequestMethod.GET)
    //@NeedLogin
    public String updateView(Model model, @PathVariable("userId") int userId) {
        User user = this.adminService.findUserById(userId);
        model.addAttribute("user", user);

        return "user/updateView";
    }

    /**
     * 
     * @param user
     * @return list.jsp
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    //@NeedLogin
    public String update(User user) {
        this.adminService.update(user);
//System.out.println("111111111111111111");
        return "user/deleteSuccess";
    }
    
    @RequestMapping(value = "/delete/{userId}", method = RequestMethod.GET)
    //@NeedLogin
    public String delete(User user, @PathVariable("userId") String userId) {
        this.adminService.delete(userId);

        return "/user/deleteSuccess";
    }
    
    @RequestMapping(value = "/addView", method = RequestMethod.GET)
   // @NeedLogin
    public String addView() {
        return "user/addView";
    }
    
    @RequestMapping(value = "/save", method = RequestMethod.POST)
   // @NeedLogin
    public String save(User user) {
        this.adminService.save(user);
        return "redirect:/user/list";
    }
    
    @RequestMapping(value="/addBookView", method = RequestMethod.GET)
    public String addBookView() {
    	return "book/addBookView";
    }
    
    @RequestMapping(value="/addBook",method = RequestMethod.POST)
    public String addBook(Book book) {
    	adminService.addBook(book);
    	return "book/addBookSuccess";
    }

    @RequestMapping(value = "/deleteBook/{bookId}", method = RequestMethod.GET)
    //@NeedLogin
    public String deleteBookById(@PathVariable("bookId") String bookId) {
        this.adminService.deleteBookById(bookId);

        return "book/deleteBookSuccess";
    }
    
    @RequestMapping(value="/home",method = RequestMethod.GET)
    public String home() {
    	return "menu/home";
    }
    
    @RequestMapping(value="/top",method = RequestMethod.GET)
    public String top() {
    	return "menu/top";
    }
    
    @RequestMapping(value="/left",method = RequestMethod.GET)
    public String left() {
    	return "menu/left";
    }
    
    @RequestMapping(value="/right",method = RequestMethod.GET)
    public String right() {
    	return "menu/right";
    }
}
